<?php
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;

/**--tmpl-use--**/
/***
 * @name : 公告
 * @Class : NoticeAction
 * @Author : horse
 * @Create : 2016-10-28
 * @copyright : Fiidee.Monda.Cn
 */

class NoticeAction extends CommonAction {

	private	 $noticeCateService;
	public function C_start()
	{
		parent::C_start();
		$this->noticeCateService = Beans::get("information.informationNoticeCate.service");
	}
	protected $serviceBean="information.informationNotice.service";

	public function add(HttpRequest $request) {
	
		parent::add($request);
		$noticeCate = $this->noticeCateService->getItems();
		$this->assign("noticeCate", $noticeCate);
		$this->assign("title", "添加公告");
		$this->assign("adminName", adminName());
		$this->assign('bread', array('信息管理','公告列表',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;',url('admin/notice/index'),'javascript:;'));
	
	}
	public function edit(HttpRequest $request) {
		parent::edit($request);
		$noticeCate = $this->noticeCateService->getItems();
		$this->assign("noticeCate", $noticeCate);
		$this->assign("title", "编辑公告");
		$this->assign("adminName", adminName());
        $this->assign('bread', array('信息管理','公告列表',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;',url('admin/notice/index'),'javascript:;'));
    }
	public function index(HttpRequest $request) {
		$conditions = array();
		$this->assign('_table','公告表');
		$s_title = $request->getParameter('s_title',"urldecode");
		$this->assign('s_title',$s_title);
		if($s_title !== '' && $s_title!== NULL)$conditions['title'] = '%'.$s_title.'%';
		
		$s_cate_id = $request->getParameter('s_cate_id',"intval");
		$this->assign('s_cate_id',$s_cate_id);
		if($s_cate_id != 0)$conditions['cate_id'] = "$s_cate_id";

		$s_s_addtime = $request->getParameter('s_s_addtime');
		$s_e_addtime = $request->getParameter('s_e_addtime');
		$this->assign('s_s_addtime',$s_s_addtime);
		$this->assign('s_e_addtime',$s_e_addtime);
		
		if($s_s_addtime !== '' && $s_s_addtime!== NULL){ 
			$conditions['addtime'] = array(); 
			$conditions['addtime']['>'] = $s_s_addtime.' 00:00:00';
		}
		
		if($s_e_addtime !== '' && $s_s_addtime!== NULL){
			if(!$conditions['addtime']){ $conditions['addtime'] = array();}
			$conditions['addtime']['<='] = $s_e_addtime.' 23:59:59';
		}

								
		$s_manager_id = $request->getParameter('s_manager_id');
		$this->assign('s_manager_id',$s_manager_id);
		if($s_manager_id !== '' && $s_manager_id!== NULL)$conditions['manager_id'] = '%'.$s_manager_id.'%';
		$s_app_id = $request->getParameter('s_app_id');
		$this->assign('s_app_id',$s_app_id);
		if($s_app_id !== '' && $s_app_id!== NULL)$conditions['app_id'] = '%'.$s_app_id.'%';
		$this->assign('_filter',array('title','content','information_notice_cate_id','addtime','admin_manager_id','app_id'));
		$this->assign('_header',array('标题','公告内容','公告类型','添加时间','操纵人','AppId'));
		$this->setConditions($conditions);

		$this->page = $request->getParameter('page', 'intval');
		if ( $this->page <=0 ) {
			$this->page = 1;
		}
		$pagesize = $request->getParameter('pagesize') ? $request->getParameter('pagesize') : $this->getPagesize();
		$order = $request->getParameter('order') ? $request->getParameter('order') : $this->getOrder();
		$order = str_replace('+' , " ",$order);
		$this->setPagesize($pagesize);
		$this->setOrder($order);
		$this->assign('order',$order);
		$this->assign('pagesize',$pagesize);
		//parent::index($request);
        $service = Beans::get($this->getServiceBean());
        $total = $service->count($this->getConditions());
        $items = $service->getItems($this->getConditions(), $this->getFields(), $this->getOrder(),
            array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());
        $this->assign('items', $items);

        //导出Excel Add By Lao liu
        if( $request -> getParameter('export') == 1 ){
            $ids = $request -> getParameter('ids');
            if( $ids ){
                $conditions['id']['$in']=$ids;
                $this -> setConditions($conditions);
            }
            $this->setPage(1);
            $this->setPagesize(2147483648);
            $items = $service->getItems($this->getConditions(), $this->getFields(), $this->getOrder(),
                array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());
            $excel = array();
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["title"];
                $tmp[] = $v["information_notice_cate_name"];
                $tmp[] = strip_tags($v["release_status"]);
                $tmp[] = $v["addtime"];
                $tmp[] = $v["admin_manager_username"];
                $excel[] = $tmp;
            }
            $header = array('标题','公告类型','状态','添加时间','操作人');
            phpexcel("公告列表",$header,$excel);
            exit();
        }

        $this->generatePageMenu($total);
		//显示分类名称
		$noticeCate = $this->noticeCateService->getItems();
		$this->assign("noticeCate", $noticeCate);
		$this->assign("cate_url", "/admin/noticeCate/index");
		$this->assign("title", "公告列表");
        $this->assign('bread', array('信息管理',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;','javascript:;'));
        $this->setView(strtolower($request->getAction()) . '/index');
    }

	public function do_add(HttpRequest $request)
	{
		$data = $request->getParameter('data');
        $data['content'] = addslashes($data['content']);
		$data['manager_id'] = adminId();
		$data['deadline'] = date("Y-m-d H:i:s",time());//数据库中有个保留字段deadline没有默认值，不设置初始值将无法插入
		$request->addParameter('data',$data);
		parent::do_add($request);	
	}

	public function do_edit(HttpRequest $request){
		$data = $request->getParameter('data');

        $data['content'] = addslashes($data['content']);
		$request->addParameter('data',$data);
		parent::do_edit($request);	
	}
	
	

}
